from rest_framework import serializers
from six_it.models import Article, User, Comment, Spit


class ArticleSerializerForCreate(serializers.ModelSerializer):
    image = serializers.CharField(required=False, default='',allow_blank=True)

    class Meta:
        model = Article
        exclude = ('collected_users',)

class ArticleSerializerSimple(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = ("id", "title")

class UserDetailSerializer(serializers.ModelSerializer):

    articles = ArticleSerializerSimple(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username','avatar','articles','fans')



class CommentSerializerItem(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    subs = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = ('id', 'content','article','user','parent','subs','createtime')

class CommentSerializerList(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    subs = CommentSerializerItem(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = "__all__"

class ArticleSerializerForDetail(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    comments = CommentSerializerList(read_only=True, many=True)

    class Meta:
        model = Article
        fields = "__all__"


class SpitDetailSerializers(serializers.ModelSerializer):
    class Meta:
        model = Spit
        fields = ('id', 'content', 'publishtime', 'userid',
                  'nickname', 'visits', 'thumbup', 'comment',
                  'avatar', 'collected', 'hasthumbup', 'parent')


class CommentSerializerForCreate(serializers.ModelSerializer):

    class Meta:
        model = Comment
        fields = ('id', 'content','article','user','parent','createtime')